<template>
  <div>
    <van-cell v-for=" (str ,index) in suggestions " :key="index" icon="search" @click="$emit('hehe',str)">
      <div slot="title" v-html="highlight(str)"> </div>
    </van-cell>
  </div>

</template>

<script>
import { getSearchSuggestions } from '@/api/search.js'
import { debounce } from 'lodash'
export default {
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      suggestions: []
    }
  },
  watch: {
    searchText: {
      handler: debounce(async function() {
        const { data: res } = await getSearchSuggestions(this.searchText)
        this.suggestions = res.data.options
      }, 200),
      immediate: true
    }
  },
  methods: {
    highlight(str) {
      const high = new RegExp(this.searchText, 'gi')
      const light = `<span style="color:red">${this.searchText}</span>`
      return str.replace(high, light)
    }
  }
}
</script>
